<template>
  <div class="content">
    <div class="head">
      <img class="imgUrl" src="https://open-cards-1302786294.cos.ap-guangzhou.myqcloud.com/images/20240624/cfc39318b3da43c1a67e675e21a6bc6f.png" alt="" />
      <div class="headContent">
        <div class="name">默认昵称</div>
        <div class="mobile">
          {{userinfo.mobile?userinfo.mobile:''}}
          <div class="submitMobile" @click=" openShowpopup">手机绑定</div>
        </div>
      </div>
    </div>
    <div class="card">
      <div class="cardHead">
        <div class="cardtitle">我的订单</div>
        <div class="cardMore" @click="gotoOrder(0)">全部<van-icon name="arrow" color="#AFAFAF"/></div>
      </div>
      <div class="cardList">
        <div class="list" @click="gotoOrder(0)">
          <img src="../../../static/icon4.png" alt="" />
          <div class="listName">待付款</div>
        </div>
        <div class="list" @click="gotoOrder(1)">
          <img src="../../../static/icon5.png" alt="" />
          <div class="listName">已付款</div>
        </div>
        <div class="list" @click="gotoOrder(2)">
          <img src="../../../static/icon6.png" alt="" />
          <div class="listName">退款</div>
        </div>
      </div>
    </div>
    <van-tabbar v-model="active" @change="onChange">
        <van-tabbar-item>
          <span>电影/影院</span>
          <template #icon="props">
            <img :src="props.active ? icon.inactive : icon.active" />
          </template>
        </van-tabbar-item>
        <van-tabbar-item>
          <span>个人中心</span>
          <template #icon="props">
            <img :src="props.active ? icon2.active : icon2.inactive" />
          </template>
        </van-tabbar-item>
    </van-tabbar>


    <div class="popup" v-if="showpopup" round>
      <div class="popups">
        <div class="changePhonePage">
          <div class="changePhoneTitle">{{ title }}</div>
          <van-row class="changePhoneView" style="">
            <van-field v-model="newPhone" label="手机号码" placeholder="请输入手机号码" />
          </van-row>
          <van-row class="changePhoneView">
            <van-cell-group inset>
              <van-field v-model="captcha" placeholder="请输入图形验证码">
                <template #button>
                  <img class="uuidImg" :src="imgUrl" @click="uuidClick" />
                </template>
              </van-field>
            </van-cell-group>
          </van-row>
          <van-row class="changePhoneView" style="margin-left: 20px;">
            <van-col class="smsCodeView">
              <van-field v-model="smsCode" label="验证码" placeholder="验证码" />
            </van-col>
            <van-col class="smsCodeText">
              <div class="smsCodeSpan" v-show="!showCountDown" @click="getSms">获取验证码</div>
              <van-count-down
                v-show="showCountDown"
                ref="countDown"
                :auto-start="false"
                @finish="countDownFinish"
                :time="time"
                format="ss秒"
                style="line-height: .64rem;color:#ff5500;"
              />
              <!-- <div>{{timeButShow?timeNum:codeText}}</div> -->
            </van-col>
          </van-row>
          <div class="changePhoneButVuew">
            <!-- <div class="changePhoneCancel" @click="phoneCancel">取消</div> -->
            <div class="changePhoneBut" @click="beforeClose">提交</div>
          </div>
        </div>

          <div @click="closePopup">
            <img class="closeImg" src="../../../static/icon12.png" alt="" />
          </div>
      </div>

    </div>
    <div class="kefu" @click="gotoKf"></div>
  </div>
</template>

<script>
import { baseApi } from '@/config'
import {
  setStore,
  getStore,
  removeStore
} from '@/utils/storage'
import {Tabbar, TabbarItem} from 'vant';
import {getPlanSeat,userInfo,smsCode,bindMobile} from '@/api/home.js'
export default {
  name: "my",
  data() {
    return {
      active: 0,
      icon: {
        active: 'https://open-cards-1302786294.cos.ap-guangzhou.myqcloud.com/images/20240624/cfc39318b3da43c1a67e675e21a6bc6f.png',
        inactive: 'https://open-cards-1302786294.cos.ap-guangzhou.myqcloud.com/images/20240624/7180fa59cbd94a559eb62b5b177eb2e7.png',
      },
      icon2: {
        active: 'https://open-cards-1302786294.cos.ap-guangzhou.myqcloud.com/images/20240624/b6b47815fe6f42fba08420bfdce47ff6.png',
        inactive: 'https://open-cards-1302786294.cos.ap-guangzhou.myqcloud.com/images/20240624/a589c4f9784e4da49e32b53531c29e92.png',
      },
      userinfo:{},
      newPhone: '',
      smsCode: '',
      time: 60000,
      showCountDown: false,
      captcha: '',
      uuid: '',
      imgUrl: '',
      title: '修改手机号码',
      showpopup:false,
    };
  },
  mounted() {
    this.active = 1
    this.userInfo()
  },
  methods: {
    userInfo(){
      userInfo().then(res=>{
        setStore('userInfo', res.data)
        this.userinfo = res.data
      })
    },
    onChange(index){
      console.log(index)
      console.log(this.active)
      if(index==0){
        this.$router.push({
          path:'/index'
        })
      }
    },
    gotoOrder(index){
      this.$router.push({
        path:'/order?index='+index
      })
    },
    openShowpopup(){
      this.showpopup = true
      this.uuidImgData()
    },
    closePopup(){
      this.showpopup = false
      this.newPhone = ''
      this.smsCode = ''
      this.captcha = ''
      this.showCountDown = false
    },
    getUUID() {
      return 'xxxxxxxxxxxx4xxxyxxxxxxxxxxxxxxx'.replace(/[xy]/g, c => {
        return (c === 'x' ? (Math.random() * 16) | 0 : 'r&0x3' | '0x8').toString(16)
      })
    },
    uuidImgData() {
      this.uuid = this.getUUID()
      this.imgUrl = baseApi + '/captcha.jpg?uuid=' + this.uuid + '&token=' + this.$getStore('token')
      console.log(this.imgUrl)
    },
    uuidClick() {
      this.uuidImgData()
    },
    phoneCancel() {
      this.cancelData()
      this.$emit('phoneCancel')
    },
    countDownFinish() {
      this.time = 60000
      this.showCountDown = false
      this.$refs.countDown.reset()
    },
    cancelData() {
      this.newPhone = ''
      this.captcha = ''
      this.uuid = ''
      this.uuidImgData()
    },
    getSms() {
      if (!/^1[3456789]\d{9}$/.test(this.newPhone)) {
        this.showToast('请输入正确的手机号码')
        return false
      }
      if (!this.captcha) {
        this.showToast('请输入图形验证码')
        return false
      }
      const data = {
        phone: this.newPhone,
        code: this.captcha,
        uuid: this.uuid,
        configId: 'note_code'
      }
      smsCode(data)
        .then(res => {
          if (res.code === 0) {
            this.showToast(res.msg)
            this.showCountDown = true
            this.$refs.countDown.start()
          } else {
            this.showToast(res.msg)
            this.uuidImgData()
          }
        })
        .catch(res => {
          this.cancelData()
          this.showToast(res.msg)
        })
    },
    beforeClose() {
      // 点击的是确认按钮
      // 校验手机号码
      if (!/^1[3456789]\d{9}$/.test(this.newPhone)) {
        this.showToast('请输入正确的手机号码')
        return false
      }
      if (!this.captcha) {
        this.showToast('请填写图形验证码')
        return false
      }
      if (!this.smsCode) {
        this.showToast('请填写短信验证码')
        return false
      }
      // 请求修改接口
      const data = {
        phone: this.newPhone,
        code: this.smsCode,
      }
      bindMobile(data)
        .then(res => {
          if (res.code == 0) {
            this.showToast(res.msg)
            this.showpopup = false
            setTimeout(()=>{
              this.userInfo()
            },1000)
            this.newPhone = ''
            this.smsCode = ''
            this.captcha = ''
            this.$forceUpdate()
          }
        })
        .catch(res => {
          this.showToast(res.msg)
          this.smsCode = ''
          this.cancelData()

        })
    },
    showToast(msg) {
      this.$toast({
        message: msg,
        position: 'center',
        duration: 2500
      })
    },
    gotoKf(){
      window.location.href = 'https://work.weixin.qq.com/kfid/kfc478918e0c9dba7f9'
    }
  },
  watch: {},
};
</script>

<style scoped lang="scss">
  .content{
    width: 100vw;
    height: 100vh;
    background: url('../../../static/icon3.png') no-repeat;
    background-size: 100% 100%;
    overflow: hidden;
    .kefu{
      width: 50px;
      height: 50px;
      background: url('../../../static/kf.png') no-repeat;
      background-size: 100% 100%;
      position: fixed;
      bottom: 300px;
      right: 10px;
    }
    .head{
      width: 100%;
      height: 67px;
      display: flex;
      align-items: center;
      box-sizing: border-box;
      padding: 0 24px;
      margin-top: 20px;
      .imgUrl{
        width: 67px;
        height: 67px;
        border-radius: 50%;
      }
      .headContent{
        margin-left: 18px;
        overflow: hidden;
        .name{
          font-size: 20px;
          color: #333333;
          margin-top: 4px;
          font-weight: 500;
        }
        .mobile{
          font-size: 12px;
          color: #333333;
          margin-top: 8px;
          display: flex;
          align-items: center;
          .submitMobile{
            width: 65px;
            height: 24px;
            background-color: rgb(66, 157, 248);
            border-radius: 12px;
            text-align: center;
            line-height: 24px;
            color: #fff;
            margin-left: 12px;
          }
        }
      }
    }
    .card{
      width: 337px;
      height: 121px;
      background-color: #fff;
      border-radius: 5px;
      margin: 14px auto 0;
      .cardHead{
        display: flex;
        width: 311px;
        height: 45px;
        border-bottom: 1px solid #EEEEEE;
        margin: 0 auto;
        line-height: 45px;
        align-items: center;
        justify-content: space-between;
        .cardtitle{
          font-weight: 500;
          font-size: 16px;
          color: #333333;
        }
        .cardMore{
          font-weight: 500;
          font-size: 14px;
          color: #AFAFAF;
        }
      }
      .cardList{
        width: 100%;
        height: 76px;
        display: flex;
        align-items: center;
        justify-content: space-between;
        box-sizing: border-box;
        padding: 0 35px;
        .list{
          width: 42px;
          img{
            display: block;
            width: 26px;
            height: 26px;
            margin: 0 auto;
          }
          .listName{
            font-weight: 500;
            font-size: 14px;
            color: #333333;
            text-align: center;
            margin-top: 6px;
          }
        }
      }
    }




    .changePhoneCancel {
      width: 110px;
      height: 30px;
      font-size: 15px;
      text-align: center;
      letter-spacing: 2px;
      font-weight: bold;
      background: #cdcdcd;
      border-radius: 15px;
      line-height: 30px;
      color: #fff;
      margin-right: 40px;
    }
    .changePhoneButVuew {
      margin-bottom: 10px;
      display: flex;
      align-items: center;
      justify-content: space-between;
    }
    .changePhoneBut {
      width: 110px;
      height: 30px;
      font-size: 15px;
      text-align: center;
      letter-spacing: 2px;
      font-weight: bold;
      background: #3289fe;
      border-radius: 15px;
      line-height: 30px;
      color: #fff;
      margin-top: 40px;
    }
    .smsCodeSpan {
      background-color: #ff5500;
      color: #ffffff;
      font-size: 13px;
      float: right;
      padding: 4px;
      line-height: 0.64rem;
    }
    .smsCodeText {
      width: 30%;
      padding-right: 10px;
      padding-top: 10px;
    }
    .smsCodeView {
      width: 70%;
    }
    .changePhoneView {
      margin-top: 6px;
      margin-bottom: 6px;
    }
    .uuidImg{
      width: 100px;
      margin-left: 20px;
    }
    .changePhoneTitle {
      font-size: 16px;
      color: #333;
      text-align: center;
      margin-top: 10px;
      font-weight: bold;
    }
    .changePhonePage {
      width: 320px;
      display: flex;
      flex-direction: column;
      align-items: center;
    }
    .popup{
      width: 100vw;
      height: 100vh;
      background-color: rgba(0, 0, 0, .3);
      position: fixed;
      top: 0;
      left: 0;
      z-index: 999;
      overflow: hidden;
    }
    .popups{
      width: 350px;
      height: 300px;
      background-color: #fff;
      border-radius: 25px;
      margin: 150px auto 0;
      position: relative;
    }
    .closeImg{
      width: 20px;
      height: 20px;
      position: absolute;
      right: 20px;
      top: 10px;
    }
  }
</style>
